﻿using System;
using System.Data;
using System.Data.Common;
using Com.Iooogle.Bms.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Com.Iooogle.Bms.Business
{
	/// <summary>
	/// 用户处理业务类。
	/// </summary>
	public class TBuglist : IBmsBusiness
	{
		#region 标准 IBmsBusiness 成员。

		/// <summary>
		/// 添加记录。
		/// </summary>
		/// <param name="entity">指定数据实体对象。</param>
		/// <returns>返回最后插入的主键值。</returns>
		public object Create(IDataEntity entity)
		{
			BuglistTable tbl = ( BuglistTable ) entity;

			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("INSERT INTO `bms_buglist`(`ProID`, `Subject`, `Description`, `OS`, `Browser`, `Priority`, `Status`, `Creater`, `Transactor`, `UpdatedTime`, `CreatedTime`) VALUES(@ProID, @Subject, @Description, @OS, @Browser, @Priority, @Status, @Creater, @Transactor, @UpdatedTime, @CreatedTime);SELECT LAST_INSERT_ID();");
			db.AddInParameter(cmd, "ProID", DbType.Int32, tbl.ProID);
			db.AddInParameter(cmd, "Subject", DbType.String, tbl.Subject);
			db.AddInParameter(cmd, "Description", DbType.String, tbl.Description);
			db.AddInParameter(cmd, "Os", DbType.Int16, tbl.Os);
			db.AddInParameter(cmd, "Browser", DbType.Int16, tbl.Browser);
			db.AddInParameter(cmd, "Priority", DbType.Int16, tbl.Priority);
			db.AddInParameter(cmd, "Status", DbType.Int32, tbl.Status);
			db.AddInParameter(cmd, "Creater", DbType.Int32, tbl.Creater);
			db.AddInParameter(cmd, "Transactor", DbType.Int32, tbl.Transactor);
			db.AddInParameter(cmd, "UpdatedTime", DbType.DateTime, tbl.UpdatedTime);
			db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, tbl.CreatedTime);
			object obj = db.ExecuteScalar(cmd);

			return obj;
		}

		/// <summary>
		/// 更新记录。
		/// </summary>
		/// <param name="entity">指定数据实体对象。</param>
		/// <returns>返回受影响的记录条数。</returns>
		public int Update(IDataEntity entity)
		{
			BuglistTable tbl = ( BuglistTable ) entity;

			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("UPDATE `bms_buglist` SET `ProID` = @ProID, `Subject` = @Subject, `Description` = @Description, `OS` = @OS, `Browser` = @Browser, `Priority` = @Priority, `Status` = @Status, `Creater` = @Creater, `Transactor` = @Transactor, `UpdatedTime` = @UpdatedTime, `CreatedTime` = @CreatedTime WHERE `BugID` = @BugID");
			db.AddInParameter(cmd, "ProID", DbType.Int32, tbl.ProID);
			db.AddInParameter(cmd, "Subject", DbType.String, tbl.Subject);
			db.AddInParameter(cmd, "Description", DbType.String, tbl.Description);
			db.AddInParameter(cmd, "Os", DbType.Int16, tbl.Os);
			db.AddInParameter(cmd, "Browser", DbType.Int16, tbl.Browser);
			db.AddInParameter(cmd, "Priority", DbType.Int16, tbl.Priority);
			db.AddInParameter(cmd, "Status", DbType.Int32, tbl.Status);
			db.AddInParameter(cmd, "Creater", DbType.Int32, tbl.Creater);
			db.AddInParameter(cmd, "Transactor", DbType.Int32, tbl.Transactor);
			db.AddInParameter(cmd, "UpdatedTime", DbType.DateTime, tbl.UpdatedTime);
			db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, tbl.CreatedTime);
			db.AddInParameter(cmd, "BugID", DbType.Int32, tbl.BugID);
			int obj = db.ExecuteNonQuery(cmd);

			return obj;
		}

		/// <summary>
		/// 删除记录。
		/// </summary>
		/// <param name="key">指定主键标识值。</param>
		public void Delete(object key)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("DELETE FROM `bms_buglist` WHERE `BugID` = @BugID");
			db.AddInParameter(cmd, "BugID", DbType.Int32, key);
			db.ExecuteNonQuery(cmd);
		}

		/// <summary>
		/// 获取全部记录。
		/// </summary>
		/// <returns></returns>
		public DataSet GetAll()
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT `BugID`, `ProID`, `Subject`, `Description`, `OS`, `Browser`, `Priority`, `Status`, `Creater`, `Transactor`, `UpdatedTime`, `CreatedTime` FROM `bms_buglist`");
			DataSet ds = db.ExecuteDataSet(cmd);

			return ds;
		}

		/// <summary>
		/// 获取数据总数。
		/// </summary>
		/// <returns></returns>
		public int GetCount()
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(*) FROM `bms_buglist`");
			Int32 obj = Convert.ToInt32(db.ExecuteScalar(cmd));

			return obj;
		}

		/// <summary>
		/// 获取单行记录。
		/// </summary>
		/// <param name="key">指定主键标识值。</param>
		/// <returns></returns>
		public IDataEntity GetRow(object key)
		{
			BuglistTable entity = null;
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT `BugID`, `ProID`, `Subject`, `Description`, `OS`, `Browser`, `Priority`, `Status`, `Creater`, `Transactor`, `UpdatedTime`, `CreatedTime` FROM `bms_buglist` WHERE `BugID` = @BugID");
			db.AddInParameter(cmd, "BugID", DbType.Int32, key);
			using (IDataReader dr = db.ExecuteReader(cmd))
			{
				if (dr.Read())
				{
					entity = new BuglistTable();
					entity.BugID = Convert.ToInt32(dr["BugID"]);
					entity.ProID = Convert.ToInt32(dr["ProID"]);
					entity.Subject = Convert.ToString(dr["Subject"]);
					entity.Description = Convert.ToString(dr["Description"]);
					entity.Os = Convert.ToInt16(dr["OS"]);
					entity.Browser = Convert.ToInt16(dr["Browser"]);
					entity.Priority = Convert.ToInt16(dr["Priority"]);
					entity.Status = Convert.ToInt32(dr["Status"]);
					entity.Creater = Convert.ToInt32(dr["Creater"]);
					entity.Transactor = Convert.ToInt32(dr["Transactor"]);
					if (!Convert.IsDBNull(dr["UpdatedTime"]))
						entity.UpdatedTime = Convert.ToDateTime(dr["UpdatedTime"]);
					if (!Convert.IsDBNull(dr["CreatedTime"]))
						entity.CreatedTime = Convert.ToDateTime(dr["CreatedTime"]);

					dr.Close();
				}
			}

			return entity;
		}

		#endregion
	}
}